/* 
 * File:   Input.cpp
 * Author: Maximiliano Monzón (82341)
 * 
 * Created on October 12, 2012, 12:29 AM
 */

#include <iostream>
#include <fstream>
#include <cstring>
#include "Input.h"

using namespace std;
using namespace TP;

Input::Input() {
    this->stream = &cin;
    this->fileName = "";
}

Input::Input(int argc, char* argv[]) throw (int) {
    
    this->stream = &cin;
    this->fileName = "?";
    
    if (argc > 1)
        this->fileName = argv[1];
}

Input::~Input() {
    this->Liberar();
}

istream& Input::Stream () const{
    return *this->stream;
}

void Input::Inicializar () throw (int){
    
    if (0 != strcmp (this->fileName.c_str(), "-"))
    {
        ifstream* file = new ifstream ();
        file->open (this->fileName.c_str(), ifstream::in);

        if (file->is_open())
            this->stream = (istream*) file;
        else
        {   
            delete file;
            throw INIT_INPUT_FAIL;
        }
    }
}

void Input::Liberar (){
    
    if (this->stream)
        if (&cin != this->stream)
        {
            ((ifstream*) this->stream)->close();
            delete ((ifstream*) this->stream);
        } 
}